<?php
date_default_timezone_set('Asia/Shanghai');
use Workerman\Worker;
require_once __DIR__ . '/Autoloader.php';
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('tcp://0.0.0.0:7777');
Worker::$stdoutFile = 'run_server.log';
$worker->count = 1;
$worker->onWorkerStart = function($data){
    echo "conn start\n";
};
$worker->onConnect = function($data){
    echo "conn finsh\n";
};
$worker->onMessage = function($con,$data) use ($worker){
    echo "get message{$data}\n";
   $data= json_decode($data,true);
   //接送来自小程序端发送的信息['status'=>'AppToServer','uid'=>$uid,'face_path'=>$face_path]
    if (!empty($data['face_code'])){
        $message=json_encode(array('code'=>'0','face_code'=>$data['face_code'],'face_path'=>$data['face_path']));
        foreach($worker->connections as $id=>$connection){
            $connection->send($message);
        }
    }else{
        foreach($worker->connections as $id=>$connection){
            $message=json_encode(array('code'=>'1','face_code'=>'','face_path'=>''));
            $connection->send($message);
        }
    }

};
$worker->onClose = function($data){
    echo "close conn\n";
};
$worker->onError = function($data){
    echo "err\n";
};
Worker::runAll();
// $postdata 是传输的数据，数组格式
function curl_post( $url, $postdata ) {
    $header = array(
        'Accept: application/json',
    );
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // 超时设置
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    // 设置请求头
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
    //执行命令
    $data = curl_exec($curl);
    // 显示错误信息
    if (curl_error($curl)) {
        print "Error: " . curl_error($curl);
    } else {
        // 打印返回的内容
        curl_close($curl);
        return $data;
    }
}
